package com.facemox.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.facemox.bean.User;
import com.facemox.service.UserService;
import com.facemox.util.JsonData;

@RestController
@RequestMapping("/contro")
public class UserController {
    
    @Autowired
    private UserService userService;
    
    //@RequestMapping("/login")
    public int login(HttpServletRequest request,HttpServletResponse response) {
        
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        
        // 1、根据用户名获取用户信息
            User user = userService.getUserByUsername(username);
        if(password.equals(user.getPassword())) {
            //response
        }
        return 0;
    }
    
    
    
    
    
    
    
    
    
    
   
    
    // 添加用户
    @RequestMapping("add")
    public Object add() {
        User user = new User();
       user.setUsername("songjianghu");
        user.setPassword("15210232015");
        user.setCreateTime(new Date());
        int id = userService.add(user);
        return new JsonData("101", "插入成功", "ID:"+id);
    }
    
    // 获取所有用户
    @RequestMapping("getAll")
    public List<User> getAll() {
        List<User> users = userService.getAll();
        return users;
    }
    
    // 根据ID获取用户
    @RequestMapping("getUser")
    public User findById() {
        User user = userService.findById("10");
        return user;
    }
    
    
    
    // 根据ID更新用户
    @RequestMapping("updateUser")
    public int updateById(User user) {
    	user.setUserId(20);
    	user.setUsername("zhangxx");
        int effectLine = userService.updateById(user);
        return effectLine;
    }
    
    // 根据ID删除用户
    @RequestMapping("deleteUser")
    public int deleteById() {
        int effectLine = userService.deleteById("20");
        return effectLine;
    }
}
